package test;


import dao.StudentMapper;
import entity.Student;
import org.apache.ibatis.session.SqlSession;
import util.MybatisUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 */
public class StudentTest {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int choice = 0;
        while (true){
            SqlSession session = MybatisUtil.get();
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-mm-dd");//可以方便地修改日期格式
            System.out.println("1.增加学生 2修改学生信息 3删除学生 4.分页查询信息 5id查询 6.查询全部 7.模糊查询 8.符合条件总数  0退出");
            choice=in.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("请输入学生姓名");
                    String sname=in.next();
                    System.out.println("请输入学生性别");
                    String sex=in.next();
                    System.out.println("请输入手机号");
                    String telephone=in.next();
                    System.out.println("请输入QQ");
                    String qq=in.next();
                    System.out.println("请输入微信");
                    String wechat=in.next();
                    System.out.println("请输入年级编号");
                    int gradeid=in.nextInt();
                    System.out.println("请输入入学日期:");
                    String enterdate1=in.next();
                    Date enterdate = null;
                    try {
                        enterdate = dateFormat.parse(enterdate1);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    System.out.println("来自哪个城市");
                    String fromcity=in.next();
                    System.out.println("出生日期");
                    String birthday1=in.next();
                    Date birthday = null;
                    try {
                        birthday = dateFormat.parse(birthday1);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    System.out.println("地址");
                    String address=in.next();
                    System.out.println("密码");
                    String pass=in.next();
                    Student stu=new Student(sname,sex,telephone,qq,wechat,gradeid,enterdate,fromcity,birthday,address,pass);
                    int a=mapper.add(stu);
                    System.out.println(a);
                    System.out.println("增加主健为："+stu.getStuno());
                    MybatisUtil.close(session);
                    break;
                case 2:
                    System.out.println("请输入要修改的id");
                    int stuno=in.nextInt();
                    System.out.println("请输入学生姓名");
                    String sname2=in.next();
                    System.out.println("请输入学生性别");
                    String sex2=in.next();
                    System.out.println("请输入手机号");
                    String telephone2=in.next();
                    System.out.println("请输入QQ");
                    String qq2=in.next();
                    System.out.println("请输入微信");
                    String wechat2=in.next();
                    System.out.println("请输入年级编号");
                    int gradeid2=in.nextInt();
                    System.out.println("请输入入学日期:");
                    String enterdate2=in.next();
                    Date enterdate3 = null;
                    try {
                        enterdate3 = dateFormat.parse(enterdate2);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    System.out.println("来自哪个城市");
                    String fromcity2=in.next();
                    System.out.println("出生日期");
                    String birthday2=in.next();
                    Date birthday3 = null;
                    try {
                        birthday3 = dateFormat.parse(birthday2);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    System.out.println("地址");
                    String address2=in.next();
                    System.out.println("密码");
                    String pass2=in.next();
                    Student stu2=new Student(stuno,sname2,sex2,telephone2,qq2,wechat2,gradeid2,enterdate3,fromcity2,birthday3,address2,pass2);
                    int a2=mapper.update(stu2);
                    System.out.println(a2);
                    MybatisUtil.close(session);
                    break;
                case 3:
                    System.out.println("请输入要删除的id");
                    int stuno3=in.nextInt();
                    int a3=mapper.del(stuno3);
                    System.out.println(a3);
                    if (a3==0){
                        System.out.println("删除失败");
                    }
                    MybatisUtil.close(session);
                    MybatisUtil.close(session);
                    break;
                case 4:
                    System.out.println("请输入第几页");
                    int ye=in.nextInt();
                    System.out.println("请输入多少行");
                    int hang=in.nextInt();
                    List<Student> all = mapper.fenye(ye,hang);
                    for (Student s : all)
                        System.out.println(s);
                    MybatisUtil.close(session);
                    break;
                case 5:
                    System.out.println("请输入要查找的学号");
                    int id=in.nextInt();
                    Student student = mapper.get(id);
                    System.out.println(student==null?"没有":student);
                    MybatisUtil.close(session);
                    break;
                case 6:
                    List<Student> list =mapper.findALL();
                    for (Student s:list){
                        System.out.println(s);
                    }
                    MybatisUtil.close(session);
                    break;
                case 7:
                    System.out.println("请输入姓名");
                    String name=in.next();
                    System.out.println("请输入qq");
                    String q=in.next();
                    System.out.println("请输入手机号");
                    String sjh=in.next();
                    System.out.println("请输入起始时间");
                    String sj1=in.next();
                    Date datesj1=null;
                    try {
                        datesj1=dateFormat.parse(sj1);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    System.out.println("请输入到哪时间");
                    String sj2=in.next();
                    Date datesj2=null;
                    try {
                        datesj2=dateFormat.parse(sj2);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    List<Student> stu1 =mapper.mohu(name,q,sjh,datesj1,datesj2);
                    for (Student s:stu1){
                        System.out.println(s);
                    }
                    MybatisUtil.close(session);
                    break;
                case 8:
                    System.out.println("请输入姓名");
                    String name8=in.next();
                    System.out.println("请输入qq");
                    String q8=in.next();
                    System.out.println("请输入手机号");
                    String sjh8=in.next();
                    System.out.println("请输入起始时间");
                    String sj18=in.next();
                    Date datesj18=null;
                    try {
                        datesj18=dateFormat.parse(sj18);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    System.out.println("请输入到哪时间");
                    String sj28=in.next();
                    Date datesj28=null;
                    try {
                        datesj28=dateFormat.parse(sj28);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    int stu8 =mapper.mohuzongshu(name8,q8,sjh8,datesj18,datesj28);
                    System.out.println(stu8);
                    MybatisUtil.close(session);
                    break;
                case 0:
                    return;
            }
        }
    }
}